package com.demo.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.reactive.config.WebFluxConfigurer;

import java.nio.charset.StandardCharsets;

/**
 * Web配置类，用于配置HTTP消息转换器的字符编码
 */
@Configuration
public class WebConfig implements WebFluxConfigurer {

    /**
     * 配置字符串消息转换器，确保使用UTF-8编码
     * @return StringHttpMessageConverter实例
     */
    @Bean
    public HttpMessageConverter<String> stringHttpMessageConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        return converter;
    }

    /**
     * 配置JSON消息转换器，确保使用UTF-8编码
     * @return MappingJackson2HttpMessageConverter实例
     */
    @Bean
    public HttpMessageConverter<Object> mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        return converter;
    }
}